package com.tenement.action;

import com.tenement.domain.TenementClient;
import com.tenement.domain.TenementType;
import com.tenement.domain.TenementUser;
import com.tenement.service.TypeService;
import com.tenement.utils.Message;
import com.tenement.utils.Tools;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : TypeAction  //类名
 * @Description : 收藏客户action //描述
 * @Author : 易嵘  //作者
 * @Date: 2020-06-07 20:31  //时间
 */
@Controller
@RequestMapping("typeAction")
public class TypeAction {

    @Resource
    TypeService typeService;

    /**
     * 收藏客户查询
     * @param type 客户模型
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @param page 当前页数
     * @param limit 显示行数
     * @param session 获取session
     * @return
     */
    @ResponseBody
    @RequestMapping("findAll")
    public Map<String,Object> findAll(TenementClient type,
                                      @RequestParam(value = "startDate",required =false) String startDate,
                                      @RequestParam(value = "endDate",required =false) String endDate,
                                      @RequestParam("page") Integer page, @RequestParam("limit") Integer limit,
                                        HttpSession session){
        TenementUser user= (TenementUser) session.getAttribute("user");
        List list=typeService.findAll(type,page,limit,startDate,endDate,user.getUserId());
        Long overCpunt=typeService.overCount(type,startDate,endDate,user.getUserId());

        return Tools.maps(overCpunt,list);
    }

    /*@ResponseBody
    @RequestMapping("selectAll")
    public List<TenementType> selectAll(){
        return typeService.selectAll();
    }*/

    /**
     * 新增Or修改
     * @param type
     * @return
     */
    @ResponseBody
    @RequestMapping("addOrUpdate")
    public Message addOrUpdate(TenementType type, HttpSession session, HttpServletRequest request) {
        TenementUser user= (TenementUser) session.getAttribute("user");
        String ip=request.getRemoteAddr();
        int row;
        if(StringUtils.isEmpty(type.getTypeId())){
            row=typeService.add(type,user,ip);
        }else{
            row=typeService.update(type,user,ip);
        }
        return Tools.rows(row);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteById")
    public Message deleteById(String id,HttpSession session,HttpServletRequest request){
        TenementUser user= (TenementUser) session.getAttribute("user");
        String ip=request.getRemoteAddr();
        int row=typeService.deleteById(id,user.getUserId(),ip);
        return Tools.rows(row);
    }

    /**
     * 单个查询
     * @param typeId
     * @return
     */
    @ResponseBody
    @RequestMapping("findById")
    public TenementType findById(String typeId){
        return typeService.findById(typeId);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteAll")
    public Message deleteAll(@RequestParam(value = "ids[]",required =false)String ids[],HttpSession session,HttpServletRequest request){
        TenementUser user= (TenementUser) session.getAttribute("user");
        String ip=request.getRemoteAddr();
        int row=typeService.deleteAll(ids,user.getUserId(),ip);
        return Tools.rows(row);
    }

    /**
     * 日期格式化
     * @param binder
     */
    /*@InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); // true:允许输入空值，false:不能为空值

    }*/

}
